/*
 * @Author: wuqingchun
 * @Date: 2024-05-24 08:51:40
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-05-24 09:41:21
 */

#include "co_connection.h"
#include "co_scheduler.h"


void AcceptCoroutine(void* pScheduler, uint32_t uiCoId)
{
    int iErr;
    int iAcceptFd;
    struct sockaddr_in stPeerAddr;
    socklen_t uiAddrLen = sizeof(stPeerAddr);
    CoScheduler* pobjScheduler = (CoScheduler*)pScheduler;
    Connection* pobjConn = (Connection*)pobjScheduler->GetCoUserData(uiCoId);

    printf("BEGIN %s\n", __FUNCTION__);

    if (pobjConn == nullptr)
    {
        return;
    }

    Connection* pobjAConn = pobjConn->Accept();
    if (pobjAConn == nullptr)
    {
        return;
    }

    if (pobjScheduler->AddConnection(*pobjAConn, EPOLLIN) != 0)
    {
        pobjAConn->Close();
        delete pobjAConn;
    }

    printf("END %s\n", __FUNCTION__);
}

void ReadCoroutine(void* pScheduler, uint32_t uiCoId)
{
    printf("BEGIN %s\n", __FUNCTION__);

    CoScheduler* pobjScheduler = (CoScheduler*)pScheduler;
    Connection* pobjConn = (Connection*)pobjScheduler->GetCoUserData(uiCoId);
    if (pobjConn == nullptr)
    {
        return;
    }

    printf("END %s\n", __FUNCTION__);
}

void WriteCoroutine(void* pScheduler, uint32_t uiCoId)
{
    printf("BEGIN %s\n", __FUNCTION__);

    CoScheduler* pobjScheduler = (CoScheduler*)pScheduler;
    Connection* pobjConn = (Connection*)pobjScheduler->GetCoUserData(uiCoId);
    if (pobjConn == nullptr)
    {
        return;
    }

    printf("END %s\n", __FUNCTION__);
}